package com.retail.order.service.impl;

import com.retail.core.entity.ParamReq;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.order.entity.request.SysAdminVerifyDetailRequest;
import com.retail.order.entity.vo.SysAdminVerifyDetailVo;
import com.retail.order.mapper.SysAdminVerifyDetailMapper;
import com.retail.order.service.SysAdminVerifyDetailService;
import com.retail.setting.entity.vo.RetailFxLevelVo;
import com.retail.system.entity.vo.AdminVo;
import com.retail.wxchat.entity.vo.RetailWxGetMoneyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户核销单子明细 Service impl
 *
 * @author Administrator
 */
@Service
public class SysAdminVerifyDetailServiceImpl implements SysAdminVerifyDetailService {

    @Resource
    private SysAdminVerifyDetailMapper sysAdminVerifyDetailMapper;
    
    @Override
    public SysAdminVerifyDetailVo getModel(Long id) {
        return sysAdminVerifyDetailMapper.getModel(id);
    }

    @Transactional
    @Override
    public Long save(SysAdminVerifyDetailRequest request) {
        sysAdminVerifyDetailMapper.save(request);
        return request.getId();
    }

    @Transactional
    @Override
    public Long update(SysAdminVerifyDetailRequest request) {
        sysAdminVerifyDetailMapper.update(request);
        return request.getId();
    }

    @Override
    public List<SysAdminVerifyDetailVo> listPage(ParamReq<SysAdminVerifyDetailRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        return sysAdminVerifyDetailMapper.listPage(params);
    }

    @Override
    public Integer countPage(ParamReq<SysAdminVerifyDetailRequest> params) {
        return sysAdminVerifyDetailMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return sysAdminVerifyDetailMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public SysAdminVerifyDetailVo getMaxModelByOrderId(String adminId, String orderId) {
        return sysAdminVerifyDetailMapper.getMaxModelByOrderId(adminId, orderId);
    }

    @Transactional
    @Override
    public Long verifyOrder(AdminVo adminVo, RetailFxLevelVo matchFxLevel, RetailWxGetMoneyVo retailWxGetMoneyVo, List<SysAdminVerifyDetailVo> orderDetails) {
        try {
            Long id = saveVerify(adminVo, matchFxLevel, retailWxGetMoneyVo, orderDetails);
            saveVerifyDetails(orderDetails, id);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @Override
    public List<SysAdminVerifyDetailVo> findDetailByVerifyId(Long verifyId) {
        return sysAdminVerifyDetailMapper.findModelByVerifyId(verifyId);
    }

    private void saveVerifyDetails(List<SysAdminVerifyDetailVo> orderDetails, Long id) {
        orderDetails.forEach(orderDetail -> {
            SysAdminVerifyDetailRequest request = new SysAdminVerifyDetailRequest();
            BeanUtils.copyProperties(orderDetail, request);
            request.setVerifyId(id);
            request.setDelFlag(DelFlagEnum.NO.getValue());
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            sysAdminVerifyDetailMapper.save(request);
        });
    }

    /**
     * 插入核销数据
     *
     * @param adminVo
     * @param matchFxLevel
     * @param retailWxGetMoneyVo
     * @param orderDetails
     * @return
     */
    private Long saveVerify(AdminVo adminVo, RetailFxLevelVo matchFxLevel, RetailWxGetMoneyVo retailWxGetMoneyVo, List<SysAdminVerifyDetailVo> orderDetails) {
        Integer ownerOrderCount = 0;

//        BigDecimal totalPrice = new BigDecimal("0");
//        BigDecimal ownerOrderPrice = new BigDecimal("0");
//        BigDecimal totalOwnerVerifPrice = new BigDecimal("0");
//
//        for (SysAdminVerifyDetailVo order : orderDetails) {
//            if (ObjectUtil.equal(order.getAdminId(), order.getOrderOwnerId() + "")) {
//                ownerOrderCount++;
//                ownerOrderPrice = ownerOrderPrice.add(order.getOrderPrice());
//                totalOwnerVerifPrice = totalOwnerVerifPrice.add(order.getOrderPrice().multiply(matchFxLevel.getFallIntoPer()).divide(new BigDecimal(100)));
//            }
//
//            if (ObjectUtil.notEqual(order.getAdminId(), order.getOrderOwnerId() + "")) {
//
//
//                BigDecimal fallIntoGroupPer = matchFxLevel.getFallIntoGroupPer();
//                if (ObjectUtil.isEmpty(fallIntoGroupPer)) {
//                    fallIntoGroupPer = new BigDecimal(0);
//                }
//            }
//
//            totalPrice = totalPrice.add(order.getOrderPrice());
//        }
//
//        SysAdminVerifyRequest sy = new SysAdminVerifyRequest();
//        sy.setVerifyTime(new Date());
//        sy.setAdminId(adminVo.getId());
//        sy.setAdminLevel(matchFxLevel.getFxName());
//        sy.setTotalPrice(totalPrice);
//
//        sy.setWechatPer(retailWxGetMoneyVo.getGetMoneyPer());
////        sy.setWechatPrice(sy.getTotalVerifPrice().multiply(sy.getWechatPer()).divide(new BigDecimal(100)));
//        sy.setExamineVerify(ExamineVerifyEnums.UN_EXAMINE_VERIFY.getValue());
//        sy.setTixianFlag(TiXianFlagEnums.DAI_TI_XIAN.getValue());
////        sy.setId();
//        sy.setDelFlag(DelFlagEnum.NO.getValue());
//        sy.setStatusFlag(StatusFlagEnum.USE.getValue());
//        sy.setCreateUser(adminVo.getId());
////        sy.setCreateTime();
////        sy.setUpdateTime();
//        sy.setRemark("用户提现");
//        sysAdminVerifyMapper.save(sy);
//        return sy.getId();
        return 1L;
    }
}
